﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OSharp.Core.Mapping;
using OSharp.Demo.Contracts;
using OSharp.Demo.Dtos.System;
using OSharp.Utility;
using OSharp.Utility.Data;
using OSharp.Web.Mvc.Security;
using OSharp.Web.Mvc.UI;

namespace Serenity.Libra.Web.Controllers
{
    [Description("管理-字典")]
    public class DictItemController : AdminBaseController
    {
        public ISystemContract SystemContract { get; set; }
        [Description("管理-字典-列表")]
        public override ActionResult Index()
        {
            return base.Index();
        }

        #region 获取数据
        [AjaxOnly]
        [Description("管理-字典-列表数据")]
        public ActionResult TreeData()
        {
            var list = SystemContract.DictTypes.ToList().MapTo<List<DictTypeOutputDto>>();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [AjaxOnly]
        [Description("管理-字典-列表数据")]
        public ActionResult DictItemList(Guid id)
        {
            var dictType = SystemContract.DictTypes.First(o => o.Id == id);
            return Json(dictType.Item.MapTo<List<DictItemOutputDto>>(), JsonRequestBehavior.AllowGet);
        }
        
        [AjaxOnly]
        [Description("管理-字典-获取信息")]
        public ActionResult DictType(Guid id)
        {
            var dictType = SystemContract.DictTypes.First(o => o.Id == id);
            return Json(dictType.MapTo<DictTypeOutputDto>(), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 功能方法

        [HttpPost]
        [AjaxOnly]
        [Description("管理-字典-新增")]
        public ActionResult Add(DictTypeInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = SystemContract.AddDictTypes(dtos);
            return Json(result.ToAjaxResult());
        }

        [HttpPost]
        [AjaxOnly]
        [Description("管理-字典-编辑")]
        public ActionResult Edit(DictTypeInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = SystemContract.EditDictTypes(dtos);
            return Json(result.ToAjaxResult());
        }

        [HttpPost]
        [AjaxOnly]
        [Description("管理-字典-删除")]
        public ActionResult Delete(Guid[] ids)
        {
            ids.CheckNotNull("ids");
            OperationResult result = SystemContract.DeleteDictTypes(ids);
            return Json(result.ToAjaxResult());
        }

        [HttpPost]
        [AjaxOnly]
        [Description("管理-字典项-新增")]
        public ActionResult AddItem(DictItemInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = SystemContract.AddDictItems(dtos);
            return Json(result.ToAjaxResult());
        }

        [HttpPost]
        [AjaxOnly]
        [Description("管理-字典项-编辑")]
        public ActionResult EditItem(DictItemInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = SystemContract.EditDictItems(dtos);
            return Json(result.ToAjaxResult());
        }

        [HttpPost]
        [AjaxOnly]
        [Description("管理-字典项-删除")]
        public ActionResult DeleteItem(Guid[] ids)
        {
            ids.CheckNotNull("ids");
            OperationResult result = SystemContract.DeleteDictItems(ids);
            return Json(result.ToAjaxResult());
        }
        #endregion
    }
}